package com.imooc.producer.a_normal.sync;

import com.utils.Const;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/4/9-2024
 * @Description: 生产者发送消息。  同步发送消息
 */
public class SyncProducer {

    public static void main(String[] args) throws Exception {
        //定义生产者
        DefaultMQProducer producer = new DefaultMQProducer("sync_producer");
        producer.setNamesrvAddr(Const.NAME_SERVER_SINGLE_LOCAL);

        //设置同步发送失败时的重试次数为3
        producer.setRetryTimesWhenSendFailed(3);

        //启动生产者
        producer.start();

        //发送10条消息。
        for (int i = 0; i < 1; i++) {
            Message message = new Message("sync_topic", "tags_" + i, "key_" + i, ("第" + i + "条消息").getBytes());
            SendResult sendResult = producer.send(message);
            System.out.println("消息发送结果" + sendResult);

            SendStatus sendStatus = sendResult.getSendStatus();
        }

        //关闭生产者
        producer.shutdown();
    }

}